/**
 * 
 */
package com.mvity.framework.common.utils;

import java.io.Serializable;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.mvity.framework.common.base.BaseUtil;

/**
 * Spring Context 工具类
 * 
 * 用于获取已定义的Spring bean实例
 * 
 * @author vity
 * @date 2012-7-22
 * @mail vity.1215@gmail.com
 */
public class SpringContextUtil extends BaseUtil implements ApplicationContextAware {

	private static final long serialVersionUID = -3175586499419629197L;

	private static SpringContextUtil contextUtil;

	private ApplicationContext context;

	public SpringContextUtil() {
		contextUtil = this;
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		context = arg0;
	}

	public static SpringContextUtil getUtil() {
		return contextUtil;
	}

	/**
	 * 获取指定ean
	 * 
	 * @param clazz
	 *            bean类型
	 * @param serviceName
	 *            业务类名称
	 * @return
	 */
	public <T extends Serializable> T getBean(Class<T> clazz, String serviceName) {
		if (context == null) {
			return null;
		}
		return context.getBean(serviceName, clazz);
	}

	/**
	 * 获取指定业务类bean
	 * 
	 * @param clazz
	 *            bean类型
	 * @return
	 */
	public <T extends Serializable> T getBean(Class<T> clazz) {
		if (context == null) {
			return null;
		}
		return context.getBean(clazz);
	}

	public Object getBean(String serviceName) {
		if (context == null) {
			return null;
		}
		return context.getBean(serviceName);
	}

}
